package com.quartz.jobModel;

import com.quartz.config.SpringDataConfig;
import com.quartz.domain.Order_Log;
import lombok.Data;
import org.quartz.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author 袁嘉乐-tram
 * * @date 2022/7/4
 */
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
@Service
@Data
public class QuartzJob extends QuartzJobBean {


    RedisTemplate redisTemplate=new SpringDataConfig().myDataRedisTemplate();

//    MongoTemplate mongoTemplate=new SpringDataConfig().myDataMongoTemplate();
    private int ID;

    private int count;

    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        try {
            System.out.println("调度器编号=" + context.getScheduler().getSchedulerInstanceId());
            System.out.println("taskname=" + context.getJobDetail().getKey().getName());
            System.out.println("执行时间=" + new Date());
            JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
            System.out.println("已定时处理用户袁,编号为[" + jobDataMap.getInt("ID") + "]的订单,共[" + jobDataMap.getInt("count") + "]订单");
            //累加
            jobDataMap.put("count", jobDataMap.getInt("count") + 1);
            //写入redis
            //TODO 每天下午3点进行redis刷新,防止缓存穿透雪崩
            this.saveCount();
            System.out.println("=============================================================================================================");
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    //Mongo 业务处理
    public void saveCount() {
//      mongoTemplate.save(new Order_Log(ID,count));
//    redisTemplate = (RedisTemplate) SpringContextUtils.getBean("myDataRedisTemplate");
      redisTemplate.opsForHash().put("订单组", String.valueOf(ID),String.valueOf(count));

    }


}
